package com.mine.frame.mylandemo.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ClickUtils
import com.mine.frame.mylandemo.R
import kotlinx.android.synthetic.main.item_ip.view.*
import java.util.*

/**
 * company 广东玛格家居有限公司
 * @author smoker
 * @Date 2021/12/17
 * Description:
 */
class IPAdapter : RecyclerView.Adapter<IPAdapter.IPViewHolder>() {

    private var itemChildClickListener:((view:View, position:Int)->Unit)? = null

    private var listIP = arrayListOf<String>()

    private var macMap = mutableMapOf<String ,String>()

    fun withIPs():Boolean = listIP.isNotEmpty()

    fun getIPList()= listIP

    fun refreshIP(list: MutableList<String>){
        this.listIP.clear()
        this.listIP.addAll(list)
        this.notifyDataSetChanged()
    }

    fun refreshMac(map: MutableMap<String,String>){
        this.macMap.clear()
        this.macMap.putAll(map)
        this.notifyDataSetChanged()
    }

    fun getIPFromPosition(position: Int):String = if (position < listIP.size && position >= 0){
        listIP[position]
    }else{
        ""
    }

    fun addChileClickListener(listener:((view:View, position:Int)->Unit)){
        this.itemChildClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IPViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_ip ,parent ,false)
        return IPViewHolder(view)
    }

    override fun onBindViewHolder(holder: IPViewHolder, position: Int) {
        holder.itemView.tvIP.text = listIP[position]
        holder.itemView.tvMac.text = macMap[listIP[position]]?.toUpperCase(Locale.CHINA)
    }

    override fun getItemCount(): Int  = listIP.size


    inner class IPViewHolder(itemView: View) :RecyclerView.ViewHolder(itemView){
        init {
            ClickUtils.applySingleDebouncing(itemView.btnConnect){view->
                itemChildClickListener?.let { it(view ,adapterPosition) }
            }
        }
    }
}